package cn.jietuo.first.class01;

import java.util.Arrays;


/**
 * @author zhangx & jietuo_zx@163.com
 * @version 1.0
 * @date 2020/5/26
 * @description: 选择排序
 * <p>
 * 第一次，从所有数中找出最小的和第一个数交换位子
 * 第二次，从剩下n-1个数中找出最小的和第二个数交换位子
 */
public class Code01_SelectionSort {

    public static void selectionSort(int[] arr) {
        if (arr == null || arr.length <= 1) {
            return;
        }
        for (int i = 0; i < arr.length - 1; i++) {
            int minIndex = i;
            for (int j = i + 1; j < arr.length; j++) {
                minIndex = arr[minIndex] < arr[j] ? minIndex : j;
            }
            swap(arr, i, minIndex);
        }
    }

    /**
     * 交换
     *
     * @param arr
     * @param i
     * @param minIndex
     */
    private static void swap(int[] arr, int i, int minIndex) {
        int temp = arr[i];
        arr[i] = arr[minIndex];
        arr[minIndex] = temp;
    }

    public static void main(String[] args) {
        // 测试次数
        int testTime = 500000;
        // 数组大小
        int maxSize = 50;
        // 最大数字
        int maxValue = 100;
        boolean succeed = true;
        for (int i = 0; i < testTime; i++) {
            // 随机生成一个数字
            int[] randomArray = generateRandomArray(maxSize, maxValue);
            int[] copyArray = copyArray(randomArray);
            selectionSort(randomArray);
            compartorSort(copyArray);
            if (!isEqual(randomArray, copyArray)) {
                succeed = false;
                printArray(randomArray);
                System.out.println();
                printArray(copyArray);
            }
        }
        System.out.println(succeed ? "Nice!" : "Oops!");
    }

    private static boolean isEqual(int[] arr1, int[] arr2) {
        if ((arr1 == null && arr2 != null) || (arr1 != null && arr2 == null)) {
            return false;
        }
        if (arr1 == null && arr2 == null) {
            return true;
        }
        if (arr1.length != arr2.length) {
            return false;
        }
        for (int i = 0; i < arr1.length; i++) {
            if (arr1[i] != arr2[i]) {
                return false;
            }
        }
        return true;
    }

    /**
     * 使用系统的排序工具
     *
     * @param copyArray
     */
    private static void compartorSort(int[] copyArray) {
        Arrays.sort(copyArray);
    }

    /**
     * 拷贝数组
     *
     * @param randomArray
     * @return
     */
    private static int[] copyArray(int[] randomArray) {
        if (randomArray == null) {
            return null;
        }
        int[] array = new int[randomArray.length];
        for (int i = 0; i < randomArray.length; i++) {
            array[i] = randomArray[i];
        }
        return array;
    }

    private static void printArray(int[] randomArray) {
        for (int i = 0; i < randomArray.length; i++) {
            System.out.print(randomArray[i] + " ");
        }
    }

    /**
     * 生成一个随机数组
     *
     * @param maxSize
     * @param maxValue
     * @return
     */
    private static int[] generateRandomArray(int maxSize, int maxValue) {
        int[] array = new int[(int) ((maxSize + 1) * Math.random())];
        for (int i = 0; i < array.length; i++) {
            array[i] = (int) ((maxValue + 1) * Math.random()) - (int) (maxValue * Math.random());
        }
        return array;
    }
}
